package com.softeem.music.controller;

import com.softeem.music.comm.QiniuOssUtils;
import com.softeem.music.comm.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

@Tag(name = "文件上传",description = "文件上传")
@RestController
public class FileController {


    @Operation(summary = "文件上传",description = "上传文件")
    @PostMapping("/file/upload")
    public R Upload(MultipartFile  file) throws IOException {
        //获取原始的文件名
        String key = file.getOriginalFilename();

        /*System.out.println("文件名:"+file.getOriginalFilename());
        System.out.println("文件大小:"+file.getSize());
        System.out.println("文件类型:"+file.getContentType());*/

        //获取文件的输入流对象
        InputStream is = file.getInputStream();

        //获取文件名最后一个"."的位置
        int i = key.lastIndexOf(".");

        //获取文件名后缀名
        String suffix = key.substring(i+1);

        //文件重命名
        key = UUID.randomUUID().toString()+"."+suffix;
        //创建七牛云存储对象
        QiniuOssUtils qiniuOssUtils = new QiniuOssUtils();

        //上传文件
        String url = qiniuOssUtils.upload(is,key);

        //返回结果
        return R.success().setData(url);
    }

}
